package aygx.Configuration.yml;

import aygx.Configuration.yml.config.MeteringConfig;
import aygx.Configuration.yml.config.YmlConfigFactory;
import com.mysql.cj.jdbc.MysqlDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

/**
 * @author 暗影孤星
 * @date 2020/10/24 23:04
 * @explanation Spring Boot 项目启动类
 * @PropertySource moren
 */
@Slf4j
@SpringBootApplication
@PropertySource(value = {"classpath:custom.properties", "classpath:custom.yml"}, factory = YmlConfigFactory.class)
public class YmlApplication {

    public static void main(String[] args) {
        // 设置默认值
//        SpringApplication application = new SpringApplication(YmlApplication.class);
//        application.setDefaultProperties(JSONUtil.createObj().set("defaultProperties", "defaultProperties......"));
//        application.run(args);

        ConfigurableApplicationContext context = SpringApplication.run(YmlApplication.class, args);
        //System.out.println(context.getBean(Person.class));
        //System.out.println(context.getBean(LooseConfig.class));
        // MeteringConfig(serverTimeOut=PT10S, dataSize=10240B)
        System.out.println(context.getBean(MeteringConfig.class));

        // custom.property.source,ZXX
        log.info("custom.property.source,{}", context.getEnvironment().getProperty("custom.property.source"));
        log.info("custom.property.source2,{}", context.getEnvironment().getProperty("custom.property.source2"));
    }

    /**
     * 为当前bean的返回值绑定值
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public MysqlDataSource dataSource() {
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setPassword("654321");
        return mysqlDataSource;
    }
}
